package subumbo.micro.view
{	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import subumbo.micro.controller.ImageEvent;
	import subumbo.micro.controller.ImageManager;

	public class Image extends Loader
	{
		private var _url:String;
		private var _priority:int
		
		public function Image(url:String, priority:int)
		{
			init(url, priority);
		}
		
		private function init(url:String, priority:int):void
		{		
			_url = url;
			_priority = priority;
			ImageManager.instance.addImage(this);
		}
		
		public function startLoading():void
		{
			load(new URLRequest(_url));
			this.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false,0 ,true);
		}
		
		private function onComplete(e:Event):void
		{
			this.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			dispatchEvent(new ImageEvent(ImageEvent.IMAGE_LOADED));
			e.stopImmediatePropagation();
		}
		
		public function get priority():int
		{
			return _priority;
		}
	}
}